package com.ljy.controller;
import com.alibaba.fastjson.JSONObject;
import com.ljy.pojo.Device;

import com.ljy.pojo.Device_type;
import com.ljy.service.Device_Service;
import com.ljy.utils.MassageJson;
import com.ljy.utils.MassageJsons;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static java.time.ZonedDateTime.now;


@RestController
@RequestMapping("/d")
public class Device_controller {

    @Resource
    Device_Service device_Service;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @RequestMapping(value = "/l")
    public MassageJsons listAdmin(){
        List<Device> deviceList = null;
        try {
            deviceList=device_Service.listdevice();
            if(deviceList!=null){
                return new MassageJsons("200","请求成功",deviceList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new MassageJsons("201","登录失败",deviceList);
    }

        //根据设备的id返回设备的状态
//    @RequestMapping(value = "/s")
//    public int selAdmin(int id){
//        Device device = null;
//        try {
//            device=device_Service.seldevice(id);
//            int states = 0;
//            if(device!=null){
//                System.out.println(device.getState());
//                return device.getState();
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return 0;
//    }


    //根据设备的id返回设备的状态redis
    @RequestMapping(value = "/s")
    public int selAdmin(int id){

        String redis_id = Integer.toString(id);
        try {

            String a = stringRedisTemplate.opsForValue().get("id"+redis_id);
            int states =  Integer.parseInt(a);
            if(states!=0){
                System.out.println(states);
                System.out.println("/d/s根据设备的id=="+id+"==返回设备的状态redis==>"+states+"<=="+now());
                return states;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }


    //根据设备的id返回设备的状态
    @RequestMapping(value = "/s1")
    public MassageJsons selDevice(int id){
        Device device = null;
        try {
            device=device_Service.seldevice(id);
            int states = 0;
            if(device!=null){
                System.out.println(device.getState());
                return new MassageJsons("200","成功",device);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new MassageJsons("201","失败",null);
    }





    @RequestMapping(value = "/upd")
    public MassageJsons updAdmin(int id,int state){
        System.out.println("/d/upd修改");
        Device device = null;
        Device_type device_type = null;
        try {
            String redis_id = Integer.toString(id);
            String redis_state = Integer.toString(id);
            stringRedisTemplate.opsForValue().set("id"+redis_id, JSONObject.toJSONString(state));

            int i  = device_Service.upddevice(id,state);
            if(i == 1){
                System.out.println("更新入数据库");
            }else {
                System.out.println("更新可能未入数据库");
            }
//            device =  device_Service.seldevice(id);
//            device_type = device.getDevice_type();
//            System.out.println("getType_id"+device_type.getType_name());
//            System.out.println("?????????"+device_type.getType_id());
//            System.out.println("getType_id"+device_type.getHardware_type());
            System.out.println("updAdmin>>>>>>>>>>>>"+i);

            String r_state = stringRedisTemplate.opsForValue().get("id"+redis_id);
            int states = Integer.parseInt(r_state);
            if(states == state){
                System.out.println("修改成功");
                return new MassageJsons("1","更改成功",1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new MassageJsons("0","失败",0);
    }


}
